package controllers

import javax.inject.Inject

import dao.chinaDao
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}

import scala.concurrent.ExecutionContext.Implicits.global

class HomeController@Inject()(chinadao:chinaDao) extends Controller{


  def home = Action{
    Ok(views.html.home.homepage())
  }


  def orderInfo = Action{implicit request=>
    Ok(views.html.home.orderInfopage())
  }

  def validCode(code:String,password:String) = Action{

    var valid = "true"

    Ok(Json.obj("valid" -> valid))
  }


  def getAllAddress = Action.async{implicit request=>
    chinadao.getAll.map{x=>
      val sheng = x.map(_.sheng).distinct
      val shi = x.filter(_.sheng == "上海市").map(_.shi).distinct
      val xian = x.filter(_.sheng == "上海市").map(_.xian).distinct
      Ok(Json.obj("sheng" -> sheng,"shi" -> shi, "xian" -> xian))
    }
  }

  def getBySheng(sheng:String) = Action.async{implicit request=>
    chinadao.getBySheng(sheng).map{x=>
      val shi = x.map(_.shi).distinct
      val xian = x.map(_.xian).distinct
      Ok(Json.obj("shi" -> shi, "xian" -> xian))
    }
  }

  def getByShi(shi:String) = Action.async{implicit request=>
    chinadao.getByShi(shi).map{x=>
      val sheng = x.map(_.sheng).distinct
      val xian = x.map(_.xian).distinct
      Ok(Json.obj("sheng" -> sheng, "xian" -> xian))
    }
  }

  def getByXian(xian:String) = Action.async{implicit request=>
    chinadao.getByXian(xian).map{x=>
      val sheng = x.map(_.sheng).distinct
      val shi = x.map(_.shi).distinct
      Ok(Json.obj("sheng" -> sheng,"shi" -> shi))
    }
  }

  def saleAfter = Action{
    Ok(views.html.home.saleafter.saleafterpage())
  }

  def changeOrder = Action{
    Ok(views.html.home.change.changeorder())
  }

  def stateLook = Action{
    Ok(views.html.home.state.statelook())
  }

}
